package lacus.wd_cinema.controller.views;

import lacus.wd_cinema.service.HallService;
import lacus.wd_cinema.service.LogService;
import lacus.wd_cinema.service.MovieService;
import lacus.wd_cinema.service.ScreeningService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 经理页面接口控制类
 * @author lacus
 * @version 0.4
 * @since 0.4
 */
@Controller
@RequestMapping("/manager")
public class ManagerViewController {

    private final MovieService movieService;
    private final ScreeningService screeningService;
    private final HallService hallService;
    private final LogService logService;

    public ManagerViewController(MovieService movieService, ScreeningService screeningService, HallService hallService, LogService logService) {
        this.movieService = movieService;
        this.screeningService = screeningService;
        this.hallService = hallService;
        this.logService = logService;
    }

    @GetMapping
    public String getHomePage() {
        return "redirect:/manager/movies";
    }

    @GetMapping("/movies")
    public String getMovieListPage(
            Model model,
            @RequestParam(defaultValue = "title") String search,
            @RequestParam(defaultValue = "") String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "6") int size) {
        model.addAttribute("search", search);
        model.addAttribute("keyword", keyword);
        model.addAttribute("currentPage", page);
        model.addAttribute("size", size);
        model.addAttribute("page",
                movieService.searchMovies(page, size, search, keyword));
        return "manager/MovieListPage";
    }

    @GetMapping("/movies/add")
    public String getMovieAddPage() {
        return "manager/MovieAddPage";
    }

    @GetMapping("/movies/{id}/edit")
    public String getMovieEditPage(
            Model model,
            @PathVariable int id) {
        model.addAttribute("movie", movieService.getMovieById(id));
        return "manager/MovieEditPage";
    }

    @GetMapping("/movies/{id}")
    public String getMovieDetailsPage(
            Model model,
            @PathVariable int id) {
        model.addAttribute("movie", movieService.getMovieById(id));
        return "manager/MovieDetailsPage";
    }

    @GetMapping("/screenings")
    public String getScreeningListPage(
            Model model,
            @RequestParam long byMovieId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "") String startDate,
            @RequestParam(defaultValue = "") String endDate) {
        model.addAttribute("movie", movieService.getMovieById(byMovieId));
        model.addAttribute("page", screeningService.searchScreeningsByMovie(
                byMovieId, page, size, startDate, endDate));
        model.addAttribute("startDate", startDate);
        model.addAttribute("endDate", endDate);
        return "manager/ScreeningListPage";
    }

    @GetMapping("/screenings/add")
    public String getScreeningAddPage(
            Model model,
            @RequestParam long byMovieId) {
        model.addAttribute("movie", movieService.getMovieById(byMovieId));
        model.addAttribute("halls", hallService.getAllHalls());
        return "manager/ScreeningAddPage";
    }

    @GetMapping("/log")
    public String getManagerLogPage(
            Model model) {
        model.addAttribute("log", logService.getManagerLog());
        return "manager/ManagerLogPage";
    }
}
